package cn.bugstack.domain.activity.service.partake;

import cn.bugstack.domain.activity.model.aggregate.CreatePartakeOrderAggregate;
import cn.bugstack.domain.activity.model.entity.ActivityEntity;
import cn.bugstack.domain.activity.model.entity.PartakeRaffleActivityEntity;
import cn.bugstack.domain.activity.model.entity.UserRaffleOrderEntity;
import cn.bugstack.domain.activity.model.valobj.ActivityStateVO;
import cn.bugstack.domain.activity.model.valobj.UserRaffleOrderStateVO;
import cn.bugstack.domain.activity.repository.IActivityRepository;
import cn.bugstack.domain.activity.service.IRaffleActivityPartakeService;
import cn.bugstack.types.enums.ResponseCode;
import cn.bugstack.types.exception.AppException;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author: Zchy
 * @Description: 参与活动的抽象实现
 * @DateTime: 12/2/2024 12:18 PM
 **/
@Slf4j
public abstract class AbstractRaffleActivityPartake implements IRaffleActivityPartakeService {

    @Resource
    protected IActivityRepository repository;

    @Override
    public UserRaffleOrderEntity createOrder(PartakeRaffleActivityEntity partakeRaffleActivityEntity) {
        Long activityId = partakeRaffleActivityEntity.getActivityId();
        String userId = partakeRaffleActivityEntity.getUserId();
        Date currentData = new Date();
        log.info("创建活动抽奖单开始 userId:{} activityId:{}", userId, activityId);
        //校验活动的状态
        ActivityEntity activityEntity = repository.queryRaffleActivityByActivityId(activityId);
        if (!ActivityStateVO.open.equals(activityEntity.getState())) {
            log.error("创建活动抽奖单失败，活动状态未开启 activityId:{} state:{}", activityId, activityEntity.getState());
            throw new AppException(ResponseCode.ACTIVITY_STATE_ERROR.getCode(), ResponseCode.ACTIVITY_STATE_ERROR.getInfo());
        }

        if (activityEntity.getBeginDateTime().after(currentData) || activityEntity.getEndDateTime().before(currentData)) {
            log.error("创建活动抽奖单失败，活动时间未开始 activityId:{} state:{}", activityId, activityEntity.getState());
            throw new AppException(ResponseCode.ACTIVITY_DATE_ERROR.getCode(), ResponseCode.ACTIVITY_DATE_ERROR.getInfo());
        }

        //查询没有被使用过的抽奖订单，有的话直接返回，不需要要额外消耗账户额度
        UserRaffleOrderEntity userRaffleOrderNoUsedEntity = repository.queryRaffleOrderNoUsed(userId, activityId);
        if (userRaffleOrderNoUsedEntity != null) {
            log.info("创建参与活动订单 userId:{} activityId:{} userRaffleOrderEntity:{}", userId, activityId, JSON.toJSONString(userRaffleOrderNoUsedEntity));
            return userRaffleOrderNoUsedEntity;
        }

        UserRaffleOrderEntity userRaffleOrderEntity = UserRaffleOrderEntity.builder()
                .userId(userId)
                .activityId(activityId)
                .activityName(activityEntity.getActivityName())
                .strategyId(activityEntity.getStrategyId())
                .orderId(RandomStringUtils.randomNumeric(12))
                .orderTime(currentData)
                .orderState(UserRaffleOrderStateVO.create)
                .build();

        CreatePartakeOrderAggregate createPartakeOrderAggregate = this.doFilterAccount(userId, activityId, currentData);
        createPartakeOrderAggregate.setUserRaffleOrderEntity(userRaffleOrderEntity);
        repository.saveCreatePartakeOrderAggregate(createPartakeOrderAggregate);

        return userRaffleOrderEntity;

    }

    protected abstract CreatePartakeOrderAggregate doFilterAccount(String userId, Long activityId, Date currentData);
}
